(function() {
    var access_token, conn, open = false;
    
    var Chat = {
        init: function(token, opts) {
            access_token = token;
            return this.connect();
        },
        connect: function() {
            conn = new WebSocket('wss://chat.facebook.com:5222');
            conn.onerror = function(e) {    
                console.log(e);
            }
            conn.onopen = function() {
                open = true;
                Chat.listen();
            }
            return conn;
        },
        listen: function() {
            conn.onmessage = function(e) {
                console.log(e);
                Chat.receive(e);
            }
        },
        send: function(message) {
            /* Add a queue if not open */
            if(open) {
                conn.send(message);
            }
        },
        receive: function() {
        
        }
    };

    window.Chat = Chat;
})();